﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Entitys;
using UIWindow.Enums;
using UIWindow.GlobalParameters;
using UIWindow.Helper;
using UIWindow.Protocols.Fins;
using UIWindow.Services.Context;

namespace UIWindow.Services.Handle
{
    public class InjectionPumpParameters : TaskEventBase<InjectionPumpParametersContext>, Lucifer.ITaskEvent<InjectionPumpParametersContext>
    {
        private DeviceGlobal _device;
        private ParameterGlobal _parameter;
        private HomePageGlobal _homepage;

        public InjectionPumpParameters(DeviceGlobal device, ParameterGlobal parameter, HomePageGlobal homePage)
        {
            _device = device;
            _parameter = parameter;
            _homepage = homePage;
        }

        public override Task Handle(InjectionPumpParametersContext context)
        {
            $"条码：[{context.BarCode}] 注液量：{context.注液量} 注液泵通信启动".RunLog(MessageLevelType.Info);
            var plc = _device.DeviceList.FirstOrDefault(t => t.Types == DeviceType.注液机).Comm;
            DateTime dateTime = DateTime.Now;
            var device = _device.DeviceList.FirstOrDefault(k => k.Types == DeviceType.注液泵);
            //DataThis:
            //    ProductionData.Meta.TableName = $"ProductionData_{dateTime:yyyy}{dateTime.GetQuarterly()}";
            //    var entity = ProductionData.Find(ProductionData._.BarCode == context.BarCode);
            //    if (entity == null)
            //    {
            //        if (dateTime.ContrastTime())
            //        {
            //            dateTime = dateTime.AddDays(-1);
            //            goto DataThis;
            //        }
            //        $"条码：[{context.BarCode}] 注液泵 找不到【{context.BarCode}】条码".RunLog(MessageLevelType.Warning);
            //        return base.Handle(context);
            //    }
            $"注液泵 获取到 条码：[{context.BarCode}] 注液量：{context.注液量} ".RunLog(MessageLevelType.Info);
            int injectionVolume = (int)(context.注液量 * 100);
            $"注液量{injectionVolume} ".RunLog(MessageLevelType.Info);
            if (device.Comm != null && device.Comm.IsConnect)
            {
                try
                {
                    device.Comm.Write(new FinsWrite()
                    {
                        DA1 = device.DA1,
                        SA1 = (byte)_parameter.RunParameter.FinsIP,
                        OperatingArea = OmronOperatingAreaType.WordDM,
                        Address = 3500,
                        ProtoLength = 2,
                        DataList = new List<ushort>() {
                            (ushort)(injectionVolume % 10000),
                            (ushort)(injectionVolume / 10000)
                        }
                    });
                    $"注液量{injectionVolume} ".RunLog(MessageLevelType.Info);
                    var protocol = new ResponseReadWrite().Handle(device.Comm.Read()) as ResponseReadWrite;
                    if (protocol != null && protocol.Error == 0 && protocol.EndCodes[0] == 0 && protocol.EndCodes[0] == 0) $"条码：[{context.BarCode}] 变量泵 数值：{injectionVolume}写入成功".RunLog(MessageLevelType.Info);
                    else
                    {
                        $"条码：[{context.BarCode}] 变量泵 数值：{injectionVolume}写入失败".RunLog(MessageLevelType.Warning);
                    }
                }
                finally { }
            }
            if (plc.Write(new Models.S7NET.S7ParamModel() { Address = context.AddressResults, Value=new byte[] { 0x00,0x01} })) $"条码：[{context.BarCode}] 注液泵--OKNG信号写入成功".RunLog(MessageLevelType.Info);
            else $"条码：[{context.BarCode}] 注液泵--OKNG信号写入失败".RunLog(MessageLevelType.Warning);
            return base.Handle(context);
        }
    }
}
